package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;

import com.dao.OrderCtrl;
//import com.entity.Order;

/**
 * Servlet implementation class AddGoodsToOrderServlet
 */
@WebServlet("/AddGoodsToOrderServlet")
public class AddGoodsToOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddGoodsToOrderServlet() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Order order= new Order();
		OrderCtrl oc = new OrderCtrl(); 
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
				
		int goods_id =Integer.parseInt(request.getParameter("goods_id"));
		int depart_id = Integer.parseInt(request.getParameter("depart_id"));
		String staff = request.getParameter("staff");
		int amount = Integer.parseInt(request.getParameter("amount"));
		String cause = request.getParameter("cause");
		String other = request.getParameter("other");
			
		System.out.println(depart_id);
		int res =oc.addGoodsToOrder(goods_id, depart_id, staff, amount, cause, other);
		if(res>0){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/order.jsp");
			requestDispatcher.forward(request, response);
						
		}else{
			
			out.print("<script>alert('fail');window.history.back();</script>");
		}
	}

}
